﻿using Common;
using NB.MGJ.Common.Utility;
using NB.MGJ.Model;
using NB.MGJ.Model.AntiFake;
using NB.MGJ.Service.AntiFake;
using NB.MGJ.Service.FWSearch;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace NB.MGJ.Web.Controllers
{
    public partial class AntiFakeController
    {
        #region 防伪验证

        AntiFakeVerifyService afs = new AntiFakeVerifyService();
        public FWSearchService fw = new FWSearchService();

        public ActionResult AntiFakeVerify()
        {
            return View();
        }
        /// <summary>
        /// 防伪验证
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public string Selest(string Code, string type = "Fw_CodeSecret")
        {
            if (Code == "" || Code == null || Code=="1")
            {
                return JsonConvert.SerializeObject(new
                {
                    results = "6"
                   
                });
            }
            string sqlWhere = "";
            LogOnUserModel userModel = LogOnUser;
            var Scodes = MiMa.Jiemi(Code, AppConst.DigitCodeKey);
            var getsearchecode = Scodes.Substring(0, AppConst.SubStrLen);
            var GetEnterId = afs.GetEnterId(getsearchecode, Code);
            if ( GetEnterId != null)
            {
                var stats = afs.states(Code);
                if (stats == 2)
                {
                    return JsonConvert.SerializeObject(new
                    {
                        results = "2"

                    });
                }
            }
            
            else if (getsearchecode == null || GetEnterId == null)
            {
                return JsonConvert.SerializeObject(new
                {
                    results = "-1",
                    res = afs.ReplyInfoService(userModel.EnterpriseId)
                });
            }//如果输入错误码
            
            
            if (GetEnterId.Length == 5 || GetEnterId.Length == 1)
            {
                GetEnterId = "000" + GetEnterId;
            }
            else if (GetEnterId.Length == 6 || GetEnterId.Length == 2)
            {
                GetEnterId = "00" + GetEnterId;
            }
            else if (GetEnterId.Length == 7 || GetEnterId.Length == 3)
            {
                GetEnterId = "0" + GetEnterId;
            }




            var reply = "";

            int err = afs.GetCode(Code);

            if (err == 0)
            {
                reply = afs.ReplyInfoService(userModel.EnterpriseId);
            }
            else
            {
                if (!string.IsNullOrEmpty(Code))
                {

                    if (!string.IsNullOrEmpty(Code))
                    {
                        sqlWhere += string.Format(@" AND {0}='{1}' ", type, Code);
                    }

                }

            }

            Code = MiMa.Jiemi(Code, AppConst.DigitCodeKey);
            string fCode = Code.Substring(0, AppConst.SubStrLen);
            string sscode = fCode.Substring(0, 4);
            int sscodes = int.Parse(sscode);
            int entid = int.Parse(fCode);
            var allotcode = afs.allotcode(Code);
            int allotcodes = int.Parse(allotcode);
            string bcode = fCode.Substring(4, AppConst.SubBatchLen);
            sqlWhere += "  AND A.EnterpriseId=" + allotcodes;
            DataTable result;
            if (allotcodes == userModel.EnterpriseId || userModel.EnterpriseId == 1)//本机构或上级机构可查
            {

                result = afs.GetCodeService(sscode, fCode, Code, bcode, sqlWhere);
            }

            else
            {
                return JsonConvert.SerializeObject(new
                {
                    results = "-1",
                    res = afs.ReplyInfoService(userModel.EnterpriseId)
                });
            }

            return JsonConvert.SerializeObject(new
            {
                results = Serialize(result),
                res = reply,
                num = afs.searchtimes(Code, GetEnterId),

            });
        }
        /// <summary>
        /// 防伪信息
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public string GetCodeLogDis(string Code)
        {
            string strCode = MiMa.Jiemi(Code.Trim(), AppConst.DigitCodeKey);
            string ecode = strCode.Substring(0, AppConst.SubStrLen);
            var GetEnterId = afs.GetEnterId(ecode, Code);
            if (GetEnterId.Length == 5 || GetEnterId.Length == 1)
            {
                GetEnterId = "000" + GetEnterId;
            }
            else if (GetEnterId.Length == 6 || GetEnterId.Length == 2)
            {
                GetEnterId = "00" + GetEnterId;
            }
            else if (GetEnterId.Length == 7 || GetEnterId.Length == 3)
            {
                GetEnterId = "0" + GetEnterId;
            }

            return JsonConvert.SerializeObject(new
            {
                info = Serialize(afs.GetCodeLogDis(Code, GetEnterId)),
                num = afs.searchtimes(Code, GetEnterId),
                firsttime = afs.FirstSearch(Code, GetEnterId)
            });
        }
        #endregion

    }
}
